<?php require VIEWPATH. ADMIN_DIR. DIRECTORY_SEPARATOR. 'header.php'?>
<style type="text/css">
	
</style>
<div class="page-header">
	<h1><?=$schoolInfo['school_name']?> 班牌管理</h1>
</div>

<div class="col-xs-12">
	<a href="#modal-brand" class="btn btn-primary show-modal" data-toggle="modal" data-id="" data-password="" data-class="">
		<i class="ace-icon fa fa-table"></i> 添加班牌
	</a>
	<a href="<?=admin_url('admin/brand_batch/' . $schoolInfo['id'])?>" class="btn btn-primary" style="margin-left:20px;">
		<i class="ace-icon fa fa-plus"></i> 批量添加班牌
	</a>
	<table id="dynamic-table" class="table table-striped table-bordered table-hover" style="margin-top:10px">
		<thead>
			<tr>
				<th>班牌ID</th>
				<th>所属校区</th>
				<th>所属班级</th>
				<th>管理密码</th>
				<th>班牌类型</th>
				<th>创建时间</th>
				<th>操作</th>
			</tr>
		</thead>
		<tbody>
		</tbody>
	</table>
</div>

<!-- 班牌弹窗 -->
<div id="modal-brand" class="modal fade">
	<div class="modal-dialog">
		<div class="modal-content">
			<div class="modal-header no-padding">
				<div class="table-header">
					<button type="button" class="close" data-dismiss="modal" aria-hidden="true">
						<span class="white">&times;</span>
					</button>
					<span id="modal-name"></span>
				</div>
			</div>
			<div class="modal-body no-padding">
				<div class="row">
					<div class="col-xs-12">
						<form class="form-horizontal mt10" id="brand-form">
							<input type="hidden" name="id" />
							<div class="form-group">
								<label class="col-sm-3 control-label no-padding-right">所属校区：</label>
								<div class="col-sm-6">
									<select class="chosen-select form-control tag-input-style" id="form-field-select-3" data-placeholder="选择班牌关联的校区" name="campus_id">
										<?php foreach($brandCampusList as $campus): ?>
											<option value="<?=$campus['id']?>"><?=$campus['campus_name']?></option>
										<?php endforeach; ?>
									</select>
								</div>
							</div>
							<div class="form-group">
								<label class="col-sm-3 control-label no-padding-right">关联班级：</label>
								<div class="col-sm-6">
									<select class="chosen-select form-control tag-input-style" id="form-field-select-3" data-placeholder="选择班牌关联的班级" name="class_id">
										<?php foreach($classList as $class): ?>
											<option value="<?=$class['id']?>"><?=$class['class_name']?></option>
										<?php endforeach; ?>
									</select>
								</div>
							</div>
							<div class="form-group">
								<label class="col-sm-3 control-label no-padding-right">管理密码：</label>
								<div class="col-sm-6">
									<input type="text" name="brand_password" class="form-control" placeholder="请填写班牌管理密码" />
								</div>
							</div>
							<div class="form-group">
								<label class="col-sm-3 control-label no-padding-right">班牌类型：</label>
								<div class="col-sm-6">
									<select class="chosen-select form-control tag-input-style" id="form-field-select-3" data-placeholder="选择班牌类型" name="type" value="1">
											<option value="<?=BRAND_TYPE_NORMAL?>"><?=transform_brand_type(BRAND_TYPE_NORMAL)?></option>
											<!-- <option value="<?=BRAND_TYPE_PROFESSIONAL?>"><?=transform_brand_type(BRAND_TYPE_PROFESSIONAL)?></option> -->
											<option value="<?=BRAND_TYPE_GRADE?>"><?=transform_brand_type(BRAND_TYPE_GRADE)?></option>
											<!-- <option value="<?=BRAND_TYPE_CANTEEN?>"><?=transform_brand_type(BRAND_TYPE_CANTEEN)?></option> -->
											<option value="<?=BRAND_TYPE_SCHOOL_GATE?>"><?=transform_brand_type(BRAND_TYPE_SCHOOL_GATE)?></option>
									</select>
								</div>
							</div>
						</form>
					</div>
				</div>
			</div>
			<div class="modal-footer no-margin-top" >
				<a href="javascript:;" class="btn btn-sm btn-primary" id="save-brand">
					<i class="ace-icon fa fa-check"></i> 保存
				</a>
			</div>
		</div>
	</div>
</div>

<?php require VIEWPATH. ADMIN_DIR. DIRECTORY_SEPARATOR. 'footer.php'?>
<script type="text/javascript">
	$(function(){
		var form = $("#brand-form");

		// 显示弹窗
		$("body").on("click", ".show-modal", function(){
			var id = $(this).data("id");
			form.find("[name=id]").val(id);
			form.find("[name=campus_id]").val($(this).data("campus"));
			form.find("[name=class_id]").val($(this).data("class"));
			var password = $(this).data("password") ? $(this).data("password") : "123456";
			form.find("[name=brand_password]").val(password);
			// 修改弹窗名称
			$("#modal-name").text(id ? '修改班牌' : '新增班牌');
			var type = $(this).data("type") ? $(this).data("type") : 0;
			form.find("[name=type]").val(type);
			// setTimeout(function(){
			// 	$('.chosen-select').chosen({
			// 		search_contains: true
			// 	}); 
			// }, 200)
		})

		// 监听新增班牌默认管理密码
		$("[name=campus_id]").change(function() {
			var campusId = $(this).val();
			var brandPawList = [];
			<?php foreach($brandPawList as $key => $value): ?>
				brandPawList["<?= $key ?>"] = "<?= $value ?>";
			<?php endforeach ?>
			$("[name=brand_password]").val(brandPawList[campusId]);
		});

		// 提交表单
		$("#save-brand").click(function(){
			form.submit();
		})
		var submitUrl = "<?=admin_url('admin/save_brand_action')?>/<?=$schoolInfo['id']?>";
		form_submit(form, submitUrl, function(res) {
			tips_alert(res.msg, res.success, function(){
				if(res.success) window.location.reload();
			});
		})

		// 班级表格
		$('#dynamic-table').dt({
			"ajaxUrl": '<?=admin_url('admin/get_brand_list')?>/<?=$schoolInfo['id']?>',
			"columns": [
	            {"data": "id"},
				{"data": "campusName"},
	            {"data": "className"},
	            {"data": "brand_password"},
				{"data": "typeName"},
	            {"data": "createtime"},
	            {"data": function(ret){
	            	var action = '<a class="show-modal green" href="#modal-brand" data-toggle="modal" data-id="'+ ret.id + '" data-campus="'+ ret.campus_id 
					+ '" data-class="'+ ret.class_id +'" data-password="'+ ret.brand_password + '" data-type="'+ ret.type +'"><i class="fa fa-edit"></i> 修改</a>';
	                return action;
	            }},
			]
		});
	})
</script>